

cc.Class({
    extends: cc.Component,

    properties: {
        //跳跃速度.
        jumpSpeed:0,
        //最小横向速度.
        minJumpHSpeed:0,
        //水平方向的最大速度.
        maxjumpHSpeed:0,
        //下落速度.
        dropSpeed:0,
        boardManagerNode:{
            default: null,
            type: cc.Node
        },
        cameraNode:{
            default: null,
            type: cc.Node
        },
        //Game.
        gameNode:{
            default: null,
            type: cc.Node
        },
        effectNode:{
            default: null,
            type: cc.Node
        },
        trailNode:{
            default: null,
            type: cc.Node
        },
        jumpAudio: cc.AudioClip,
        dropAudio: cc.AudioClip,
        whiteboardAudio: cc.AudioClip,
        redboardAudio: cc.AudioClip,
        dieAudio: cc.AudioClip
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        //manager.enabledDebugDraw = true;
        //到达最高层数.
        this.reachedFloor = 0;
        this.currentFloor = 0;
        //是否可以跳跃(两种状态:可跳跃/可下降).
        this.canJump = true;
        this.rigidBody = this.getComponent(cc.RigidBody);
        this.physicsCollider = this.getComponent(cc.PhysicsBoxCollider);
        this.boardManager = this.boardManagerNode.getComponent('BoardManager');
        this.cameraFollow = this.cameraNode.getComponent('CameraFollow');
        this.renderer = this.node.getChildByName('Render');
        this.animation = this.renderer.getComponent(cc.Animation);
        this.game = this.gameNode.getComponent('Game');
        this.effectManager = this.effectNode.getComponent('EffectManager');
        // this.trail = this.node.getChildByName('Trail').getComponent(cc.ParticleSystem);
        //原始的重力倍数.
        this.gravityScale = this.rigidBody.gravityScale;
        this.isDropping = false;
    },

    start () {

    },

    //update (dt) {},

    onCollisionEnter: function(other, self){
        //检测碰撞,设置触发器状态.
        if((other.tag == 2 || other.tag == 9) && !this.isVelocityPositive() && this.node.getPositionY() - 60 > other.node.getPositionY()){
            this.node.setPosition(cc.v2(this.node.getPositionX(),other.node.getPositionY()+94.65));
            this.boardNode = other.node;
            //设置降落层数并且判断.
            var currentFloor = other.getComponent('Board').floorNum;
            Global.currentFloor = currentFloor;
            if(this.isDropping){
                //急速下降.
                //摄像机抖动.
                this.cameraFollow.shake();
            }
            //判断是否是升高了.
            if(currentFloor > this.reachedFloor){
                //设置最高层.
                this.setHighestFloor(currentFloor);
                //读光的位置以及触发范围.
                //分数增加.
                var width = other.getComponent('Board').getLightNodeWidth();
                var pos = other.getComponent('Board').getLightNodePosition();
                //console.log('self:' + this.node.getPositionX() + ',pos:' + pos + ',width:' + width)
                if(self.node.getPositionX() >= pos - width / 2 && self.node.getPositionX() <= pos + width / 2){
                    //落在了加分范围.
                    //更新分数(加2分).
                    this.game.scoreCombo();
                    this.game.addScore(this.game.combo);
                    //相对于板子的位置.
                    var effectPos = cc.v2((this.node.getPositionX() - other.node.getPositionX())/other.node.scaleX,other.getComponent('Board').getSurfacePosition());
                    this.effectManager.createHit(effectPos, other.node);

                    this.playAudio(this.redboardAudio);
                }
                else{
                    this.game.clearCombo();
                    this.game.addScore(1);

                    this.playAudio(this.whiteboardAudio);
                }
                
            }
            else{
                this.playAudio(this.whiteboardAudio);
            }
            this.resetState(other);
            this.animation.play('jump_start');
        }
    },

    //落在板子上后的状态调整.
    resetState(other){
        //碰撞到了板子.
        this.canJump = true;
        this.isDropping = false;
        //强行调整旋转角.
        this.animation.stop();
        this.renderer.rotation = 0;
        this.rigidBody.linearVelocity = other.getComponent(cc.RigidBody).linearVelocity;
        //this.rigidBody.linearVelocity = cc.v2(0,0);
        //改变颜色.
        this.renderer.color = other.node.color;
        // this.trail.startColor = other.node.color;
        // this.trail.endColor = new cc.Color(other.node.color.r,other.node.color.g,other.node.color.b,0);
        //this.trailNode.active = false;
    },

    //设置现在到达的最高层.
    setHighestFloor(floor){
        this.reachedFloor = floor;
        Global.floorCount = floor;
        //进行判断是否需要新生成.
        this.boardManager.judgeCreateState(floor);
        if(floor == 1){
            //第一次跳跃在板子上.
            return;
        }
        //更新摄像机的位置.
        this.cameraFollow.updateCameraPos(this.node.getPositionY(), this.isDropping);
    },

    //跳跃动作(dstPos:屏幕点击的目标位置Vec2)
    //1.0.3版本：改成随机方向.
    jump(click){
        this.canJump = false;
        //随机的横向速度.
        var hSpeed = this.minJumpHSpeed + cc.random0To1() * (this.maxjumpHSpeed- this.minJumpHSpeed);
        var dir = 1;
        if(cc.random0To1() > 0.5){
            dir = -1;
        }
        if(click == 0){
            //新手提示往上跳跃.
            dir = 0;
        }
        var dir = cc.v2(hSpeed * dir, this.jumpSpeed);
        //改变刚体的速度.
        this.rigidBody.linearVelocity = dir;
        //播放动画.
        this.animation.play('player_rotate');
        //烟特效.
        this.effectManager.createDust(this.node.position);

        this.playAudio(this.jumpAudio);
    },

    //下降.
    drop(){
        this.isDropping = true;

        this.animation.stop();
        this.renderer.rotation = 0;
        this.rigidBody.linearVelocity = cc.v2(0,-this.dropSpeed);

        //显示拖尾.
        //this.trailNode.active = true;

        //特效.
        this.effectManager.createDown(cc.v2(0,0),this.node);

        this.playAudio(this.dropAudio);
    },

    die(){
        //这部分已经写在外面了.
        this.playAudio(this.dieAudio);
    },

    //收到指令后执行的操作.
    doAction(dstPos){
        if(this.canJump){
            //this.animation.play('jump_start');
            //动画播放完毕后起跳.
            this.jump();
        }
        else{
            this.drop();
        }
    },

    //竖直速度的方向.
    isVelocityPositive(){
        if(this.rigidBody.linearVelocity.y > 0){
            return true;
        }
        else{
            return false;
        }
    },

    setVelocity(v){
        if(!this.canJump) return;
        this.rigidBody.linearVelocity = v;
    },

    //能够穿越板子.
    canCrossBoard(){
        return this.rigidBody.linearVelocity.y >= 0;
    },

    //在空中暂停.
    pause(){
        this.rigidBody.gravityScale = 0;
        this.rigidBody.linearVelocity = cc.v2(0,0);
        this.animation.pause();
        // //隐藏拖尾防止闪烁.
        // this.trail.node.active = false;
    },

    //从空中复原至正常状态.
    play(){
        //复原重力.
        this.rigidBody.gravityScale = this.gravityScale;
        // //显示拖尾.
        // this.trail.node.active = true;
    },


    playAudio(clip){
        //TODO:设置了播放音效.
        if(clip != null && Global.bgmOn){
            cc.audioEngine.play(clip, false, 1);
        }
        
    }
});
